package com.wolfalpha.utron.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.IdeaDetailActivity;
import com.wolfalpha.utron.adapter.ItemIdeaAdapter;
import com.wolfalpha.utron.app.AppConfig;
import com.wolfalpha.utron.bean.ItemIdea;
import com.wolfalpha.utron.service.IdeaService;
import com.wolfalpha.utron.service.impl.IdeaServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.list.WaterDropListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luodon on 15/8/17.
 */
public class MainIdea extends Fragment implements WaterDropListView.IWaterDropListViewListener {

    //对父Activity的接口
    private MainInIdeaListener mlistener;
    private WaterDropListView listView;
    private IdeaService ideaService;


    private List<ItemIdea> ideas;
    private ItemIdeaAdapter adapter;

    private final int LOAD_MORE = 1;    //加载更多
    private final int LOAD_REFRESH = 2; //刷新
    private final int SHOW_STR = 3; //显示信息
    private final int NO_DATA = 4; //没有数据

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LOAD_MORE:

                    break;
                case LOAD_REFRESH:

                    break;
                case SHOW_STR:
                    UIHelper.ToastMessage(getActivity(), String.valueOf(msg.arg1));
                    break;
                case NO_DATA:
                    UIHelper.ToastMessage(getActivity(), "没有数据");
                    listView.stopRefresh();
                    listView.setPullLoadEnable(false);
                    break;
            }
        }
    };

    private int page = 1;                                       //页码

    private int way = 0;                                        //排序方式 0：时间；1：热度；2：方向 3：随机

    private List<Integer> dirs = new ArrayList<Integer>();      //方向排序的所有方向

    /**
     * 按方向排序
     *
     * @param dirss
     */
    public void sortByDirs(List<Integer> dirss) {
        way = 2;
        dirs = dirss;
        onRefresh();
    }

    /**
     * 按时间排序
     */
    public void sortByTime() {
        way = 0;
        onRefresh();
    }

    /**
     * 按热点排序
     */
    public void sortByHot() {
        way = 1;
        onRefresh();
    }

    /**
     * 随机排序
     */
    public void sortByRand() {
        way = 3;
        onRefresh();
    }

    @Override
    public void onRefresh() {
        page = 1;

        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                ideas = ideaService.getList(page, way, dirs);
                if (ideas != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (ideas != null) {
                                adapter.setData(ideas);
                                if (ideas.size() < AppConfig.LIST_PAGE_SIZE) {
                                    listView.setPullLoadEnable(false);
                                } else {
                                    listView.setPullLoadEnable(true);
                                }
                            } else {
                                listView.setPullLoadEnable(false);
                                adapter.setData(new ArrayList<ItemIdea>());
                                UIHelper.ToastMessage(getActivity(), "没有数据");
                            }
                            listView.stopRefresh();
                        }
                    });
                } else {
                    handler.sendEmptyMessage(NO_DATA);
                }
            }
        });

    }

    @Override
    public void onLoadMore() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                final List<ItemIdea> mideas = ideaService.getList(++page, way, dirs);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mideas != null) {
                            ideas.addAll(mideas);
                            adapter.setData(ideas);
                            if (mideas.size() < AppConfig.LIST_PAGE_SIZE) {
                                listView.setPullLoadEnable(false);
                            } else {
                                listView.setPullLoadEnable(true);
                            }
                        } else {
                            page--;
                            listView.setPullLoadEnable(false);
                            UIHelper.ToastMessage(getActivity(), "没有数据");
                        }
                        listView.stopLoadMore();
                    }
                });

            }
        });
    }


    public interface MainInIdeaListener {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_idea_list, container, false);

        listView = (WaterDropListView) view.findViewById(R.id.wl_idea);
        ideaService = new IdeaServiceImpl();
        listView.setWaterDropListViewListener(this);
        listView.setPullLoadEnable(false);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > ideas.size()) {
                    //过滤刷新头部
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("ideaId", ideas.get(position - 1).getId());
                    UIHelper.showActivity(getActivity(), IdeaDetailActivity.class, bundle);
                }
            }
        });
        initData();
        return view;
    }

    /**
     * 加载数据
     */
    private void initData() {
        ideas = new ArrayList<ItemIdea>();
        adapter = new ItemIdeaAdapter(getActivity(), ideas);
        listView.setAdapter(adapter);
        onRefresh();
    }

    public void setMainInIdeaListener(MainInIdeaListener listener) {
        this.mlistener = listener;
    }


}
